<?php
namespace App\Services\Trade;

use Illuminate\Support\Facades\DB;
use App\Models\Base\DistrictModel;
use App\Models\Base\UserModel;
use App\Models\Trade\Shop\ShopModel;
use App\Models\Trade\Shop\ShopCountModel;

class ShopService
{

    /**
     * 获取查询字段（列表）
     *
     * @return string
     */
    protected function makeFieldList()
    {
        return implode(',', [
            'd.id,d.user_id,d.verify_classify,d.verify_name,d.verify_type',
            'd.contact_name,d.kind_supply,d.kind_demand,d.created,d.updated',
            'd.status,d.province,p.name as province_name,d.city,c.name as city_name',
            'u.avatar,dc.supply_doing as count_supply,dc.demand_doing as count_demand',
            '(dc.supply_pv + dc.demand_pv) as count_pv,dc.collection as count_collection'
        ]);
    }

    /**
     * 获取查询字段（列表）
     * 
     * @return string
     */
    protected function makeFieldDetail()
    {
        return implode(',', [
            $this->makeFieldList(),
            'd.contact_mobile,a.name as area_name,t.name as town_name,d.address'
        ]);
    }
    
    /**
     * 获取列表数据
     * 
     * @param int $page 页码
     * @param int $pageSize 每页数量
     * @param array $param 其他参数
     * @param int $order 排序
     * @return array(\Illuminate\Support\Collection, int)
     */
    public function selectList($page = 1, $pageSize = 50, $param = array(), $order = ShopModel::ORDER_DEFAULT)
    {
        $queryList = ShopModel::dbTable('d');
        $queryList->where(function ($query) use ($param) {
            $query->where('d.kind_supply', '<>', null)
            ->orWhere('d.kind_demand', '<>', null);
        });
        $queryCount = clone $queryList;
        
        $where[] = ['d.status', ShopModel::STATUS_DEFAULT];

        // 查询数量
        $count = $queryCount->where($where)->count();
        // 构造语句
        $queryList->select(DB::raw($this->makeFieldList()))
            ->leftJoin(DistrictModel::TABLENAME . ' as p', 'p.id', '=', 'd.province')
            ->leftJoin(DistrictModel::TABLENAME . ' as c', 'c.id', '=', 'd.city')
            ->leftJoin(UserModel::TABLENAME . ' as u', 'u.id', '=', 'd.user_id')
            ->leftJoin(ShopCountModel::TABLENAME . ' as dc', 'dc.shop_id', '=', 'd.id')
            ->where($where)
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize);
        // 排序
        switch ($order) {
            case ShopModel::ORDER_HOT_DESC:
                $queryList->orderBy('count_pv', 'desc');
                break;
            case ShopModel::ORDER_USER_DESC:
                $queryList->orderBy('d.updated', 'desc');
                break;
            default:
                $queryList->orderBy('d.updated', 'desc');
                break;
        }
        // 查询数据
        $collection = $queryList->get();
        
        return array(
            $collection,
            $count
        );
    }
    
    /**
     * 获取详情数据
     *
     * @param int $id 数据ID
     * @return \Illuminate\Support\Collection
     */
    public function findDetail($id)
    {
        $where[] = ['d.id', $id];
        return ShopModel::dbTable('d')->select(DB::raw($this->makeFieldDetail()))
            ->leftJoin(DistrictModel::TABLENAME . ' as p', 'p.id', '=', 'd.province')
            ->leftJoin(DistrictModel::TABLENAME . ' as c', 'c.id', '=', 'd.city')
            ->leftJoin(DistrictModel::TABLENAME . ' as a', 'a.id', '=', 'd.area')
            ->leftJoin(DistrictModel::TABLENAME . ' as t', 't.id', '=', 'd.town')
            ->leftJoin(UserModel::TABLENAME . ' as u', 'u.id', '=', 'd.user_id')
            ->leftJoin(ShopCountModel::TABLENAME . ' as dc', 'dc.shop_id', '=', 'd.id')
            ->where($where)
            ->get();
    }
}